package org.jeecg.cn.edu.scau.vm.image.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.shiro.SecurityUtils;
import org.jeecg.cn.edu.scau.vm.common.consts.ImageConst;
import org.jeecg.cn.edu.scau.vm.common.consts.RedisConst;
import org.jeecg.cn.edu.scau.vm.common.utils.RedisIdWorker;
import org.jeecg.cn.edu.scau.vm.image.domain.VmImage;
import org.jeecg.cn.edu.scau.vm.image.enums.VmImageStatusEnum;
import org.jeecg.cn.edu.scau.vm.image.mapper.VmImageMapper;
import org.jeecg.cn.edu.scau.vm.image.service.VmImageService;
import org.jeecg.cn.edu.scau.vm.image.vo.VmImageListVo;
import org.jeecg.common.system.vo.LoginUser;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class VmImageServiceImpl
        extends ServiceImpl<VmImageMapper, VmImage> implements VmImageService {
    @Resource
    private VmImageMapper vmImageMapper;

    @Resource
    private RedisIdWorker redisIdWorker;

    @Override
    public VmImage getByImageId(String imageId) {
        QueryWrapper<VmImage> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("image_id", imageId);
        return vmImageMapper.selectOne(queryWrapper);
    }

    @Override
    public void reviewPass(VmImage vmImage) {
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        vmImage.setUpdateBy(loginUser.getId());
        vmImage.setReviewer(loginUser.getId());
        vmImage.setStatus(VmImageStatusEnum.IMAGE_PASSED.getCode());
        this.updateById(vmImage);
    }

    @Override
    public void reviewReject(VmImage vmImage) {
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        vmImage.setUpdateBy(loginUser.getId());
        vmImage.setReviewer(loginUser.getId());
        vmImage.setStatus(VmImageStatusEnum.IMAGE_REJECT.getCode());
        this.updateById(vmImage);
    }

    @Override
    public void saveImage(VmImage vmImage) {
        // 注入基础属性
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        vmImage.setUpdateBy(loginUser.getId());
        vmImage.setCreateBy(loginUser.getId());
        vmImage.setUpdateTime(LocalDateTime.now());
        vmImage.setCreateTime(LocalDateTime.now());
        vmImage.setSysOrgCode(loginUser.getOrgCode());
        // 自己生成此ImageID
        vmImage.setImageId(redisIdWorker.nextId(RedisConst.IMAGE_ID_PREFIX));
        this.save(vmImage);
    }


    @Override
    public IPage<VmImageListVo> pageListByStatus(Page<VmImage> page, Integer status) {
        QueryWrapper<VmImage> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", status);
        return pageList(page, queryWrapper);
    }

    @Override
    public void submitReview(String id) {
        // 修改状态为待审核
        vmImageMapper.updateStatusById(id, VmImageStatusEnum.IMAGE_WAITING.getCode());
    }


    @Override
    public IPage<VmImageListVo> pageList(Page<VmImage> page, QueryWrapper<VmImage> queryWrapper) {
        Page<VmImage> rawPage = this.page(page, queryWrapper);
        // 1. 获取原始数据
        List<VmImage> records = rawPage.getRecords();
        List<VmImageListVo> collect = records.stream().map((vmImage) -> {
            VmImageListVo vmImageListVo = new VmImageListVo();
            BeanUtils.copyProperties(vmImage, vmImageListVo);
            // 2. 填充额外属性
            VmImageStatusEnum enumByCode = ImageConst.getEnumByCode(vmImageListVo.getStatus());
            vmImageListVo.setImageStatusName(enumByCode.getMessage());
            vmImageListVo.setImageStatusType(enumByCode.getStatusType());
            return vmImageListVo;
        }).collect(Collectors.toList());
        // 3. 封装page对象
        Page<VmImageListVo> vmImageListVoPage = new Page<>();
        BeanUtils.copyProperties(rawPage, vmImageListVoPage, "records");
        vmImageListVoPage.setRecords(collect);
        return vmImageListVoPage;
    }
}
